#include "StdAfx.h"
#include "SDLApp.h"

SDLApp::SDLApp(void)
{
	m_title = NULL;
	m_icon = NULL;
}

SDLApp::SDLApp( int width,int height,int depth,Uint32 flags )
{
	m_width = width;
	m_height = height;
	m_depth = depth;
	m_flags = flags;
	m_title = NULL;
	m_icon = NULL;
}
SDLApp::~SDLApp(void)
{
	SDL_FreeSurface(m_surface);
	SDL_Quit();
}

void SDLApp::SetVideoMode( int width,int height,int depth,Uint32 flags )
{
	m_width = width;
	m_height = height;
	m_depth = depth;
	m_flags = flags;
	m_running = false;
}

int SDLApp::Run()
{
	if (OnInit()==false)
	{
		return -1;
	}
	m_running = true;
	SDL_Event Event;
	while(m_running)
	{
		while(SDL_PollEvent(&Event))
		{
			OnEvent(&Event);
		}
		OnLoop();
		OnRender();
		Sleep(0);
	}
	OnExit();
	return 0;
}


void SDLApp::SetTitle(char* title)
{
	m_title = title;
}

void SDLApp::SetTitle(char* title,char* icon)
{
	m_title = title;
	m_icon = icon;
}

bool SDLApp::OnInit()
{
	if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_TIMER)<0)
	{
		return false;
	}
	if ((m_surface=SDL_SetVideoMode(m_width,m_height,m_depth,m_flags))=NULL)
	{
		return false;
	}
	SDL_WM_SetCaption(m_title,m_icon);
	return true;
}

void SDLApp::OnEvent( SDL_Event* Event )
{
	SDLEvent::OnEvent(Event);
}

void SDLApp::OnLoop()
{

}

void SDLApp::OnRender()
{
	
}

void SDLApp::OnExit()
{
	m_running = false;
}